<template>
  <div class="login">
    <el-card v-show="!register" shadow="always">
      <template #header>
        <div class="card-header">
          <p style="color:#8D92A1">快速登录千城百行招聘网，找工作，找人才，更方便！</p>
        </div>
      </template>
      <div style="width: 700px;padding: 0">
        <el-row>
          <el-col :span="8">
            <div style="padding: 10px;margin: 38px 10px 20px 10px">
              <el-row :gutter="20">
                <el-col :span="6" style="color: #D1D4DB">
                  <i class="el-icon-s-comment" style="font-size: 40px"></i>
                </el-col>
                <el-col :span="16">
                  <span style="color: #8D92A1">找工作</span><br>
                  <span style="color: #C3C7D0;font-size: 12px">千城百行任你选</span>
                </el-col>
              </el-row>
            </div>

            <div style="padding: 10px;margin: 21px 10px">
              <el-row :gutter="20">
                <el-col :span="6" style="color: #D1D4DB">
                  <i class="el-icon-s-custom" style="font-size: 40px"></i>
                </el-col>
                <el-col :span="16">
                  <span style="color: #8D92A1">找人才</span><br>
                  <span style="color: #C3C7D0;font-size: 12px">各大行业大牛聚集</span>
                </el-col>
              </el-row>
            </div>

            <div style="padding: 10px;margin: 21px 10px">
              <el-row :gutter="20">
                <el-col :span="6" style="color: #D1D4DB">
                  <i class="el-icon-s-cooperation" style="font-size: 40px"></i>
                </el-col>
                <el-col :span="16">
                  <span style="color: #8D92A1">任性选</span><br>
                  <span style="color: #C3C7D0;font-size: 12px">各大行业职位任你选</span>
                </el-col>
              </el-row>
            </div>

            <div style="padding: 10px;margin: 21px 10px">
              <el-row :gutter="20">
                <el-col :span="6" style="color: #D1D4DB">
                  <i class="el-icon-s-claim" style="font-size: 40px"></i>
                </el-col>
                <el-col :span="16">
                  <span style="color: #8D92A1">人才匹配度高</span><br>
                  <span style="color: #C3C7D0;font-size: 12px">获取更精准的牛人</span>
                </el-col>
              </el-row>
            </div>
          </el-col>
          <el-col :span="16" style="border-left: solid 1px #e2e2e2;padding-left: 30px">
              <div v-show="!captchaLogin">
                <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
                  <h3 class="title">千城百行招聘网</h3>
                  <el-form-item prop="username">
                    <el-input
                        v-model="loginForm.username"
                        type="text"
                        auto-complete="off"
                        placeholder="账号"
                    >
                      <span slot="prefix" style="font-size:18px" class="el-icon-user"></span>
                    </el-input>
                  </el-form-item>
                  <el-form-item prop="password">
                    <el-input
                        v-model="loginForm.password"
                        type="password"
                        auto-complete="off"
                        placeholder="密码"
                        @keyup.enter.native="handleLogin"
                    >
                      <span slot="prefix" style="font-size:18px" class="el-icon-lock"></span>
                    </el-input>
                  </el-form-item>
                  <el-form-item prop="code">
                    <el-input
                        v-model="loginForm.code"
                        auto-complete="off"
                        placeholder="验证码"
                        style="width: 63%"
                        @keyup.enter.native="handleLogin"
                    >
                      <span slot="prefix" style="font-size:18px" class="iconfont icon-yanzhengma"></span>
                    </el-input>
                    <div class="login-code">
                      <img :src="codeUrl" @click="getCode" class="login-code-img" alt=""/>
                    </div>
                  </el-form-item>
                  <el-checkbox v-model="loginForm.rememberMe" style="margin:0 0 25px 0;" size="medium">记住密码
                  </el-checkbox>
                  <el-form-item style="width:100%;">
                    <el-button
                        :loading="loading"
                        type="primary"
                        style="width:100%;"
                        @click.native.prevent="handleLogin"
                    >
                      <span v-if="!loading">登 录</span>
                      <span v-else>登 录 中...</span>
                    </el-button>
                    <div style="width:100%;">
                      <el-link class="link-type" style="float: right;" :underline="false" type="info" @click="register=true">立即注册</el-link>
                      <el-link class="link-type" style="float:left;" :underline="false" type="info"
                               @click="captchaLogin=true">手机验证码登录
                      </el-link>
                    </div>
                  </el-form-item>
                </el-form>
              </div>
              <div v-show="captchaLogin">
                <el-form ref="phoneLoginForm" :model="phoneLoginForm" :rules="phoneLoginRules" class="login-form">
                  <h3 class="title">千城百行招聘网</h3>
                  <el-form-item prop="tel">
                    <el-input
                        v-model="phoneLoginForm.tel"
                        type="text"
                        auto-complete="off"
                        placeholder="手机号"
                    >
                      <span slot="prefix" style="font-size:18px" class="el-icon-mobile-phone"></span>
                    </el-input>
                  </el-form-item>
                  <el-form-item prop="verify">
                    <el-dialog :visible.sync="visible" :close-on-click-modal="false" width="290px">
                      <el-input class="verifyInput" v-model="phoneLoginForm.verify"
                                style="visibility: hidden; height:1px;line-height: 1px;"></el-input>
                      <drag-verify-img-chip ref="dragVerify" :imgsrc="imgsrc" :isPassing.sync="isPassing"
                                            :showRefresh="true"
                                            :barWidth="40" text="请按住滑块拖动" successText="验证通过"
                                            handlerIcon="el-icon-d-arrow-right"
                                            successIcon="el-icon-circle-check" @refresh="reimg" @passcallback="pass">
                      </drag-verify-img-chip>
                    </el-dialog>
                    <el-button :disabled=dis @click="slideVerify" style="width:100%;height:45px" plain
                               :type="clickVerifyType"><i v-show="this.phoneLoginForm.verify===1"
                                                          class="el-icon-success"></i>{{ clickVerify }}
                    </el-button>
                  </el-form-item>
                  <el-form-item prop="code">
                    <el-input
                        v-model="phoneLoginForm.code"
                        auto-complete="off"
                        placeholder="短信验证码"
                        style="width: 63%"
                        @keyup.enter.native="phoneHandleLogin"
                    >
                      <span slot="prefix" style="font-size:18px" class="el-icon-message"></span>
                    </el-input>
                    <div class="login-code">
                      <el-button :disabled="this.showNum" style="height:45px;width:100%;" type="info" plain  @click="send" class="btn_number" v-bind:class="{gray:wait_timer>0}">
                        <span class="num_green" v-show="showNum" v-bind:class="{num:wait_timer>0}">{{ this.wait_timer + "s " }}</span>
                        <span class="span_number" v-bind:class="{gray_span:wait_timer>0}">{{ getCodeText() }}</span>
                      </el-button>
                    </div>
                  </el-form-item>
                  <el-checkbox style="margin:0 0 25px 0;visibility:hidden" size="medium"></el-checkbox>
                  <el-form-item style="width:100%;">
                    <el-button
                        :loading="loading"
                        type="primary"
                        style="width:100%;"
                        @click.native.prevent="phoneHandleLogin"
                    >
                      <span v-if="!loading">登 录</span>
                      <span v-else>登 录 中...</span>
                    </el-button>
                    <div style="width:100%;">
                      <el-link class="link-type" style="float: right;" :underline="false" type="info" @click="register=true">立即注册</el-link>
                      <el-link class="link-type" style="float:left;" :underline="false" type="info" @click="trans">
                        账号密码登录
                      </el-link>
                    </div>
                  </el-form-item>
                </el-form>
              </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
    <Register v-show="register" v-on:toLogin="receiveRegister"></Register>
  </div>
</template>

<script>
import dragVerifyImgChip from 'vue-drag-verify-img-chip'
import Cookies from "js-cookie";
import {encrypt, decrypt} from '@/utils/jsencrypt'
import Register from "@/views/home/Register";
import {getCodeImg} from "@/network/recruit";
import {sendCaptcha} from "@/network/user";
export default {
  name: "Login",
  components: {
    dragVerifyImgChip,
    Register
  },
  data() {
    return {
      codeUrl: '',
      wait_timer: false,
      showNum: false,
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        tel: '',
      },
      phoneLoginForm: {
        code: "",
        tel: '',
        verify: '',
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        code: [{required: true, trigger: "blur", message: "请输入验证码"}]
      },
      phoneLoginRules: {
        tel: [
          {required: true, trigger: "blur", message: "请输入您的手机号"}, {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/,
            trigger: "blur", message: '请输入正确的手机号'
          }
        ],
        code: [{required: true, trigger: "blur", message: "请输入验证码"}],
        verify: [{type: 'number', trigger: "blur", message: "请完成验证"}]
      },
      loading: false,
      // 验证码登录开关
      captchaLogin: false
      , isPassing: false,
      imgsrc: "http://celiang.oss-cn-hangzhou.aliyuncs.com/measurement/2022-04/26/uAFwlphpXmYIyH1650941449220426.jpg",
      visible: false,
      clickVerify: '点击进行验证',
      clickVerifyType: 'info',
      dis: false,
      register:false
    };
  },
  created() {
     this.getCode();
     this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg(new Date()).then(res => {
          this.codeUrl =res.data;
      });
    },
    getCookie() {
       const username = Cookies.get("username");
       const password = Cookies.get("password");
       const rememberMe = Cookies.get('rememberMe')
       this.loginForm = {
         username: username === undefined ? this.loginForm.username : username,
         password: password === undefined ? this.loginForm.password : decrypt(password),
         rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
       };
    },
    handleLogin() {
       this.$refs.loginForm.validate(valid => {
         if (valid) {
           this.loading = true;
           if (this.loginForm.rememberMe) {
             Cookies.set("username", this.loginForm.username, {expires: 30});
             Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
             Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
           } else {
             Cookies.remove("username");
             Cookies.remove("password");
             Cookies.remove('rememberMe');
           }
           this.$store.dispatch("user/login", this.loginForm).then(() => {

           }).catch(() => {
             this.loading = false;
             this.getCode();
           });
         }
       });
    },
    phoneHandleLogin() {
      this.$refs.phoneLoginForm.validate(valid => {
        this.loading = true;
        if (valid) {
          this.$store.dispatch("user/phoneLogin", this.phoneLoginForm).then(() => {
          }).catch(() => {
            this.loading = false;
          });
        }
      })
    },
    slideVerify() {
      this.visible = true;
      this.isPassing = false;
      this.phoneLoginForm.verify = '';
    },
    reimg() {

    },
    pass() {
      this.phoneLoginForm.verify = 1;
      this.clickVerifyType = 'primary';
      this.clickVerify = '已完成验证';
      this.dis = true;
    },
    reset() {
      this.isPassing = false;
      this.$refs.dragVerify.reset();
    },
    trans() {
      this.captchaLogin = false
      this.phoneLoginForm = {};
      this.loginForm = {};
    },
    receiveRegister(register){
      this.register=register;
    },
    send() {
      var reg = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/;
      if(reg.test(this.phoneLoginForm.tel)){
        sendCaptcha(this.phoneLoginForm.tel).then(res=>{
          if(res.data!==undefined){
            this.$message.success(res.data.data);
          }
        });
        this.showNum = true;
        this.wait_timer = 60;
        var that = this;
        var timer_interval = setInterval(function() {
          if (that.wait_timer > 0) {
            that.wait_timer--;
          } else {
            clearInterval(timer_interval);
          }
        }, 1000);
      }else{
        this.$message.warning("手机号错误");
      }
    },
    getCodeText: function() {
      if (this.wait_timer > 0) {
        return "已发送";
      }
      if (this.wait_timer === 0) {
        this.showNum = false;
        return "重新发送";
      }
      if (this.wait_timer === false) {
        return "发送验证码";
      }
    },
  },
};
</script>

<style scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url("../../assets/img/login-background.png");
  background-size: cover;
  background-repeat: no-repeat;
  height:721px;
  overflow: hidden;
}

.title {
  margin: 0 auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 340px;
  padding: 25px 25px 12px 40px;
}


.login-code {
  width: 33%;
  height: 45px;
  float: right;
}

/deep/ .el-input__inner {
  height: 45px;
  line-height: 45px;
  font-size: 16px;
}

/deep/ .el-dialog__body {
  padding: 0 20px 20px 20px;
}

.login-code-img {
  height: 45px;
  width: 100%;
}

/deep/ .el-tabs__nav-wrap {
  overflow: hidden;
  margin: 0 14px -1px 0;
  position: relative;
}
</style>
